<template>
	<view class="friends-list-block" :class="{'has-switch': hasSwitch}">
		<view class="search-result-tips" v-if="isSearchResult">搜索结果</view>
		<view class="name-letter" v-if="!isSearchResult">{{letter}}</view>
		<view class="friends-item" v-for="item in list" :key="item.userId" @tap="itemClk(item)">
			<view class="item-left">
				<image class="friends-avatar" :src="getImgUrl(item.userAvatar)"></image>
				<text class="friends-name">{{item.userName}}</text>
			</view>
			<view class="item-right">
				<view v-if="hasSwitch" class="switch" :class="{ active: selectedIdsInner.indexOf(item.userId) >= 0 }" @tap.stop="toggleSwitch(item.userId)">
					<view class="switch-dot"></view>
				</view>
			</view>
		</view>
		<view class="no-data" v-if="list.length === 0">
			<image class="no-data-img" src="../../static/noDataIcon.png"></image>
			<text class="no-data-tips">列表为空</text>
		</view>	
	</view>
	
</template>

<script>
	export default {
		name: "friendsListBlock",
		props:{
			letter:{
				type: String,
				default: ''
			},
			isSearchResult:{
                type: Boolean,
				default: false
			},
			hasSwitch:{
                type: Boolean,
				default: false
			},
			list:{
				type: Array,
				default(){
					return []
				}
			},
			selectedIds:{
				type: Array,
				default(){
					return []
				}
			},
		},
		data() {
			return {
                selectedIdsInner:[]
			};
		},
		methods: {
			itemClk(item) {
				this.$emit('itemClk',item)
			},
			toggleSwitch(id){
				const index = this.selectedIdsInner.indexOf(id)
				if (index !== -1) {
					this.selectedIdsInner.splice(index, 1)
				} else {
					this.selectedIdsInner.push(id)
				}
                this.$emit('switch', this.selectedIdsInner.map(item=>item))
			}
		},
		watch:{
			selectedIds(newVal){
                this.selectedIdsInner = newVal.map(item=>item)
			}
		}
	}
</script>

<style lang="less">
	.friends-list-block {
		margin-top: 26rpx;
		padding: 0 54rpx;
	
		&.has-switch{
		  padding-right: 100rpx;	
		}

		.name-letter {
			font-size: 32rpx;
			color: #333333;
			line-height: 44rpx;
			margin-bottom: 4rpx;
		}

		.search-result-tips {
			font-size: 32rpx;
			color: #999;
			line-height: 44rpx;
			margin-bottom: 4rpx;
		}

		.friends-item {
			height: 128rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.item-left {
				display: flex;
				align-items: center;

				.friends-avatar {
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
				}

				.friends-name {
					margin-left: 18rpx;
					font-size: 32rpx;
					color: #333;
					line-height: 44rpx;
				}
			}

			.item-right {
				.switch {
					width: 80rpx;
					height: 46rpx;
					border-radius: 23rpx;
					background-color: #DEDEDE;
					position: relative;

					&.active {
						background-color: #3399FF;

						.switch-dot {
							left: 38rpx;
						}
					}

					.switch-dot {
						width: 40rpx;
						height: 40rpx;
						position: absolute;
						background-color: #FFFFFF;
						border-radius: 50%;
						top: 3rpx;
						left: 4rpx;
					}
				}
			}

		}
	}
	.no-data{
		margin-top: 50rpx;
		height: 300rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		.no-data-img{
			width: 200rpx;
			height: 200rpx;
		}
		.no-data-tips{
            font-size: 26rpx;
            color: #999;
		}
	}
</style>
